package fate

import (
	"fmt"
	"strings"

	"github.com/godcong/chronos"
)

// ZodiacPig ...
const (
	ZodiacRat     = "鼠"
	ZodiacCow     = "牛"
	ZodiacTiger   = "虎"
	ZodiacRabbit  = "兔"
	ZodiacDragon  = "龙"
	ZodiacSnake   = "蛇"
	ZodiacHorse   = "马"
	ZodiacSheep   = "羊"
	ZodiacMonkey  = "猴"
	ZodiacChicken = "鸡"
	ZodiacDog     = "狗"
	ZodiacPig     = "猪"
)

//Zodiac 生肖
type Zodiac struct {
	Name      string
	Xi        string //喜
	XiRadical string
	Ji        string //忌
	JiRadical string
}

var zodiacList = map[string]Zodiac{
	ZodiacRat: {
		Name: ZodiacRat,
		Ji:   `丁二人士仁仃仇仍今介化午夭孔巴日火以付仕代仙仟卯央平旦未汀伙伊伍休仲件任仰仳份企光全印合地在圭妃寺早旨旬旭朴次竹羊臣行仵价伂位住伫伴佛何估佐佑伺伸似但作伯低伶余佈免坊址坍均坎壮孝宋巫志攸杜赤辛佟抑亚佯依侍佳使供例来佰佩仑佾侑兔味命坪坡坦妹岱幸旺易昌昆昂明昀昏昊昇东杵炎祀卧佼佶侄坵旻炅耵肖拒沫亭亮信侯侠保促俟俊俗俐係俞勉南哇型垠垣垢城姜姿宣封庠律徉春昭映昧是星昱柯柏柳炫为炳炬炯红美订酊香俋昶炷肯洋倍俸倩倖俩值倚倨俱倡候修倪俾伦冤冻卿埂埔埃姬娩宰差徐恙时晋晏晃晁书桓柴氧烊烈留羔耿袁袂酒钉马珋倢埕埒晟邦那迎胡苎苣茉英茆挽珠停假偃偌做伟健偶侦倏冕曼唱域坚堆埠基堵执婚张得从悠教晚晤曹勗欲焉烯瓷祥羚聊袋许顶偈偅偩偮娅欷欸羕邱迪悻傢傅备杰喜尧堪场堤堰报堡复普晰晴晶景智曾棚款焰然善翔贷距辜集冯傌媄焯羢郎郁脩莘莫荷提扬佣傲传仅僇吗塑塘涂塚塔填塌块坞妈微意暗晖暖暄会楠杨歇照煜焕义羡群详路钾铆驰驯渼羟郝连造逢菟署僧像侨境垫墅寿彻榴歌熙熊监绽台赫輓驳墉杩都逸进慢漾漫亿仪僵价侬俭刘墟增坠墩嬉德徵暮样槽楼欧羯蝴卖辉锌养驻驷驶驾驹鲁儆僾儌墡禡羬陈运道达潜骂儒傧壁垦坛壅曆晓昙炽燕熹笃糕羲谘醒骆嶪烨羱阳邹远优偿储壕壑壎曙营灿镁鍚骋骏鲜鲑邬蓨澲燨膳丛垒曜柠欤缮缯题骑镏骐郑邓选薑嚥垄坜曝羶羹辞鹏鄯薘繨邺迈壤曦耀议骞腾骝牺驱蓦欢鑑鞑骄骅惊驿验坝骤驞骥骊`,
	},
	ZodiacCow: {
		Name: ZodiacCow,
		Ji:   `丁人刀匕大干仁什仃仇今介化午天夭心戈支斤日月比片牙王爿丙主以付仕代令仙刊加功仟占古召叱央它弗必旦未末玉矛矢示汀伙伊伍休仲件任仰仳份企光全列刑合宇年式戎早旨旭有朱此竹米羊行衣仵价伂忙托位住佇伴佛何估佐佑伺伸佔似但作伯低伶余佈别判君呀吟坎巫希庇弟彤形忌志忍攸杉系赤辛佟礽快玖些佯依侍佳使供例来佰佩仑佾侑刻券刷到制协卓味命奈妹宗冈岱岳幸弦忠忽念或戕旺易昌昆昂明昀昏昕昊昇服朋枝杵欣版状直知社祀祁纠卧初采青忻玕佼佶侄佌旻炘炅耵罕育怔怖怪怕怡性沫玨玟玫玥表亭信侯保促俟俊俗俐係前剋则勇南奎奐姜宣庠弭彦徉思急怎怨春昭映昧是星昱柔架柏矜祉祈祇穿紂红纪纫紇约紆美羿衫订酊玡俋姝昶柰祅紈肯芯恰恢恆恃恬指洋洧珊玲珍珀玳倍俸倩倖俩值倚倨俱倡候修倪俾伦刚夏娥宰峻峰差恙恣恐恕恩时晋晏晃晁书朔朗栩格氧烊矩祕祐祠祟祖神祝秘纺纱纹素索纯纽级紜纳纸纷羔翁耿袂衽託躬钉马倢倧扆晟祜紘紓衿衾邪胖胥胡胞胤苧茅茉苓悄悟悍悔悦悖班琉珮珠停假偃偌做伟健偶侦倏副务勘动曼唱唬国婚将崇崙彬彩彫御悉悠教旋晚晤晨曹勗望桿梧欲烯祥祭絃统扎绍紼细绅组终羚翎习聆彪被袒袖袍袋许责顶珖玼偈偅偩偮崟欷欸紵紾羕袗胭脉能悽情悻惜惟悸掛採捺浅琅球理现傢傅备杰剴创劳胜场堤报彭悲惠斯普晰晴晶景智曾期朝棕椅栈棹棉款牌番短结绒绝紫絮丝络给绚善翔翕裁裂视诊费贷辜集须冯琁珺傌媄斌牋宁矞絪絜羢軫陀郁脩莘莫庄慨惶愉提扬琪琳琥琴琯琦琨佣傲传仅僇勤势吗嗣妈愚意慈感想爱愁愈愍戡暗暉暖暄会业楠杨歇照牒祺禄禁经绢绥义羡群裟裙补裘装裕详试路钾雉零驰驯愃渼琮絻羥郡郝慎慌慄慍愧準瑚瑟瑞瑙瑛瑜署僧像侨划寥廖彰慇态截畅歌碧禎福祸粽绽綰综绰綾绿紧缀网纲綺绸绵綵纶维绪緇綬臧裴裸製褚赫赶驳瑋榪綪緁緆緋綖綦裱魠慷慢惯惭漾漫瑶琐玛瑰亿仪僵价儂俭剧刘剑增嬉审影德慧慰慾暮样槽桨欧奖莹缔练纬緻缄缅编缘缎缓緲緹羯翩褐复褓褊质辉锌养驻駟驶驾驹鲁齿摎漻儆僾儌墡禡緗羬褌褋褙陈达怜憎愤潜澎璋璃璀骂儒儐剂憩战历晓曇炽燕御篤糕縑縈县縝縉羲褪裤褫醒骆璇璉嶪縕羱錼阳胶蒋忆憾璞优偿储励嶸弥应懂恳懋戏戴曙墙矫禧禪绩繆缕总纵縴縵翼褸镁鍚骋骏鲜蓨澲燨蟉襁膳蕙环璨丛断曜柠歟璧礼织缮绕繚绣繒顏题骑鄢蕥騏际臆薑璿嚥坏曝牘璽疆矇祷繫绎绳绘缴羶羹襠襟识辞鄯薘繯繨鄴藏怀琼劝悬曦朧繽继耀议騫腾龄繾瓏儷樱牺缠续驱驀惧瓔弯欢禳衬韃骄驊鷚恋晒缨纤惊驛验襴鷸陇骤鑫驞纘湾驥缆驤驪`,
	},
	ZodiacTiger: {
		Name: ZodiacTiger,
		Ji:   `一二人几三凡口士小川已弓丰之仁仇仍今介化夭少尤屯引日乏以付仕仗代仙兄冉加包仟可古右召司另只史台句央尼平弘弗旦民生田由甲申皮示禾氾伙伊伍休仲件任仰仳份企光全再危吉同各向名合后因回妃如存尖帆早旨旭朵次米臣虫仵价伂圮汎亨位住伴佛何估佐佑伺伸佃似但作伯低伶余佈克吾吴呈吕告含吟尾巫廷弟彷役攸杏甫男甸私秀谷豆贝辰邑佟冏町礽艾亚享依侍佳使供例来佰佩仑佾侏侑卷味咖咕呻和周命固坤奈委季宗宛尚岱延弦往旺易昌昆昂明昀昏昊昇知社祀祁秉卧虎门佼佶侄侗旻炅拒招河泓治泡泛亭亮信侯侠俏保促侣俟俊俗俐係俞咨咸品哈姿宣客巷建弭律思春昭映昧是星昱柯柏炬炯畏界祉祈祇禹科秒秋虹贞页风香芊芃俋昶柰祅迅巡芳芝芙芹花芬芸芷倍俸倩俩值倚倨俱倡候修倪俾伦仓唐哥哲员娟姬孙容宸庭弱徐时晋晏晃晁书桐畔留益砷祕祐祠祟祖神祝租秦秩秘素虔袁讯财贡起躬酒闪高芮珅倢倧晟祜邢邦那迎近胡范苣若茂茉苒苗英苔苑苓苯捐假偃偌做伟健偶侦倏凰区曼商哑唱唯啤售国坚堂婉婚崇庸张强得从徕悠启晚晤晨曹勗梁欲烯瓷略畦毕异祥祭移绍细绅处彪蛇蛉袖袍袋责顶麦苻偈偅偩偮埏娅欷欸邵邱述迦迪荆荐草茵荏兹茹茶茗荀茱荃捺涎傢傅备杰凯喧喜单唤乔喉场堤堠一媛富巽幅弼彭复循敦普晰晴晶景智曾替棕棻款番稍程税稀粟结给善虚蛟视费贺贵贸贷超距酥闵开闲间闳项顺须黄黍茜傌郎郁送迷迺脩莎莞莘莫莒庄莓莉荷荻莆提扬渭佣傲传仅僇募嗣园微暗晖暖暄会杨歇照当畸祺禄禁万稜稚羡号蜀蜕蜂资贾农钾钜钿预颂鼓莩琬媴郝通连速造逢逖途菩萍菁华菱著莱萌菽菲菊搭溶猿瑛署僧像侨僎嘉团图寿梦幕彻畅歌熔熙监祯福祸种称粽精综台蜜蜻诱宾闺闽颇鸣凤逑菖菇溒榬蜒郭都逵週逸进蒂落萱葵叶葛董慢漠漫亿仪僵价侬俭增娇德徵慕暮槽欧皱稿稼稽稷蝴蝶诞竖赋贤卖赐辉颉鲁黎萲儆僾儌稹陈乡运游道达违过遁蓉蓄蒙莅蒲蓓苍撰潜澎潘儒俦傧器战曆晓暹晔昙桦桥卢御积穆糕糖臻萤融谘谓讽赖醒颐龙蓁蒨螈錼阳邹远逊遣遥递蔽蔚莲蔓蒋蔡蓬挡优偿储弥懂曙档禧禅穗临襄辕鍚闆邬蓨蔘鄞适迁荡蕃蕉萧曜欤礼穠粮缯蝉丰鎔阙颜题鹃荞鎱际邓选迟薪薑薛薇蓟荐嚥曝畴疆祷穫稳譁赞关靡类颠薆譔还迈邀藏萨蓝藉薰薹严曦耀麵铧馔邈藩艺藤俪顾蓦边藻苹苏蕴弯欢叠禳鑑邝沣骅兰晒显蘩嘱蘴萝蛮逻湾`,
	},
	ZodiacRabbit: {
		Name: ZodiacRabbit,
	},
	ZodiacDragon: {
		Name: ZodiacDragon,
	},
	ZodiacSnake: {
		Name: ZodiacSnake,
	},
	ZodiacHorse: {
		Name: ZodiacHorse,
	},
	ZodiacSheep: {
		Name: ZodiacSheep,
	},
	ZodiacMonkey: {
		Name: ZodiacMonkey,
	},
	ZodiacChicken: {
		Name: ZodiacChicken,
	},
	ZodiacDog: {
		Name: ZodiacDog,
	},
	ZodiacPig: {
		Name:      ZodiacPig,
		Xi:        "",
		XiRadical: "",
		Ji:        `一乙几刀力匕三上凡也土大川己已巳干弓之仁什今天太巴引戈支斤日比片牙王爿主乏代刊加功包卉占古召叱央它市布平弘弗旦玉申皮矛矢石示氾光列刑危夷妃州帆式戎早旨旭朵此虫血衣圮托汎伸佔但伯伶别判君呀坎壮希庇廷弟彤形杉系邑礽玖些依例刻券刷到制协卓卷呻坤奇奈宗宛延弦或戕旺易昌昆昂明昀昏昕昊昇枝欣版状直知社祀祁纠初采长忻玕佌旻炘炅罕泓泡泛玫表亮侯係前剋则勇垣奐宣巷帝帅建弭彦春昭映昧是星昱架柏矜祉祈祇禹穿竿紂红纪纫紇约紆羿虹衫风玡玠柰祅紈迅巡芝芽指珊玲珍珀玳倡候修刚奚孙家差席师庭时晋晏晃晁书核栩矩砷祕祐祠祟祖神祝祚纺纱纹素索纯纽级紜纳纸纷翁蚩袁袂衽讯託起躬珅倧扆祜紘紓衿衾邪邦那迎返近范茅苓班琉珮珠乾健凰副务勘曼唱唬婉婚将崇常张强彬彩彫旋晚晤晨曹勗桿烯祥票祭絃统扎绍紼细绅组终羚翌翎聆彪蛉被袒袖袍袋责玼珩埏紵紾袗邵述迦迪能情掛採捺涎浅琅球理现剴创劳喉场堤堠媛嵐巽帧弼彭斯普晰晴晶景智曾棕牌番短结绒绝紫絮丝络给绚絳翕蛟裁裂视诊费贵须邰琁珺牋矞絪絜軫陀郎郁送迷迺莫庄莉提扬琪琳琥琴琦琨勤势园戡暗暉暖暄会杨枫照煜牒祺禄禁经绢绥群蜀蛾蜕蜂裟裙补裘装裕试钾雉零琬琰琝媴絻郡通连速造透逢途準猿瑚瑟瑞瑙瑛瑜署僎划寥廖彰截畅碧禎福祸粽绽綰综绰綾绿紧缀网纲綺绸绵綵纶维绪緇綬臧蜜蜻裳裴裹裸製褚豪宾赶闽溒瑄瑋榬綪緁緆緋綖綦蜒裱魠郭都週逸进慢漫瑶琐玛瑰剧刘剑增审影暮槽桨毅奖莹稼缔练纬緻缄缅编缘缎缓緲緹翩蝴蝶褐复褓褊诞赏质辉驻鲁齿摎漻褌褋褙陈乡运游道达违遁撰潜澎璋璃瑾璀剂勋战历晓曇炽御縑縈县縝縉萤融褪裤褫讽醒骇璇璉縕螈錼阳邹远逊遣遥递蒋璟璞励弥戏戴曙墙矫禧禪绩繆缕总纵縴縵翼襄褸辕鍚鄔蟉襁适迁环璦璨断曜璧织缮绕繚绣繒蝉顏题蕥鎱际郑邓选迟薑璿嚥曝牘璽疆祷繫绎绳绘缴襠襟识赞譔还迈邀藏琼劝曦繽继耀腾龄繾饌邈瓏樱缠续边瓔弯禳衬鷚晒缨纤襴鷸蛮纘逻湾缆驪別壯協狀糾長則帥彥紅紀紉約風剛孫師時晉書紡紗紋純紐級納紙紛訊務將張強統紮紹細紳組終責淺現創勞場幀結絨絕絲絡給絢視診費貴須莊揚勢園會楊楓祿經絹綏蛻補裝試鉀連劃暢禍綻綜綽綠緊綴網綱綢綿綸維緒賓趕閩進瑤瑣瑪劇劉劍審槳獎瑩締練緯緘緬編緣緞緩複誕賞質輝駐魯齒陳鄉運遊達違潛劑勳戰曆曉熾禦縣螢褲諷駭陽鄒遠遜遙遞蔣勵彌戲牆矯績縷總縱轅適遷環斷織繕繞繡蟬題際鄭鄧選遲禱繹繩繪繳識贊還邁瓊勸繼騰齡櫻纏續邊彎襯曬纓纖蠻邏灣纜`,
		JiRadical: "",
	},
}

var zodiacSupportList = map[string]bool{
	ZodiacRat:     true,
	ZodiacCow:     true,
	ZodiacTiger:   false,
	ZodiacRabbit:  false,
	ZodiacDragon:  false,
	ZodiacSnake:   false,
	ZodiacHorse:   false,
	ZodiacSheep:   false,
	ZodiacMonkey:  false,
	ZodiacChicken: false,
	ZodiacDog:     false,
	ZodiacPig:     true,
}

// GetZodiac ...
func GetZodiac(c chronos.Calendar) *Zodiac {
	z := chronos.GetZodiac(c.Lunar())
	b := zodiacSupportList[z]
	if !b {
		fmt.Println("[Warning]生肖:", z, ",该生肖库资料尚未补足")
	}
	if v, b := zodiacList[z]; b {
		return &v
	}
	return nil
}

func (z *Zodiac) zodiacJi(character *Character) int {
	if strings.IndexRune(z.Ji, []rune(character.Ch)[0]) != -1 {
		return -3
	}
	return 0
}

func filterZodiac(c chronos.Calendar, chars ...*Character) bool {
	return GetZodiac(c).PointCheck(3, chars...)
}

//PointCheck 检查point
func (z *Zodiac) PointCheck(limit int, chars ...*Character) bool {
	for _, c := range chars {
		if z.Point(c) < limit {
			return false
		}
	}
	return true
}

//Point 喜忌对冲，理论上喜忌都有的话，最好不要选给1，忌给0，喜给5，都没有给3
func (z *Zodiac) Point(character *Character) int {
	dp := 3
	dp += z.zodiacJi(character)
	dp += z.zodiacXi(character)
	return dp
}

func (z *Zodiac) zodiacXi(character *Character) int {
	if strings.IndexRune(z.Xi, []rune(character.Ch)[0]) != -1 {
		return 2
	}
	return 0
}
